package com.alibaba.otter.node.common.autoconfigure;

import com.alibaba.otter.node.common.communication.NodeCommunicationClient;
import com.alibaba.otter.node.common.config.ConfigClientService;
import com.alibaba.otter.node.common.config.NodeTaskService;
import com.alibaba.otter.node.common.config.impl.ConfigClientServiceImpl;
import com.alibaba.otter.node.common.config.impl.InternalConfigClientService;
import com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl;
import com.alibaba.otter.node.common.statistics.StatisticsClientService;
import com.alibaba.otter.node.common.statistics.impl.StatisticsClientServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration(proxyBeanMethods = false)
public class NodeConfigAutoConfiguration {


    @Bean
    @DependsOn("nodeCommunicationEndpoint")
    public NodeTaskService nodeTaskService(NodeCommunicationClient nodeCommunicationClient,
                                           ConfigClientService configClientService){
        NodeTaskServiceImpl nodeTaskService = new NodeTaskServiceImpl();
        nodeTaskService.setConfigClientService((InternalConfigClientService) configClientService);
        nodeTaskService.setNodeCommunicationClient(nodeCommunicationClient);
        return nodeTaskService;
    }

    @Bean
    @DependsOn("zookeeperClient")
    public ConfigClientService configClientService(NodeCommunicationClient nodeCommunicationClient){
        ConfigClientServiceImpl configClientService = new ConfigClientServiceImpl();
        configClientService.setTimeout(3600000L);
        configClientService.setNodeCommunicationClient(nodeCommunicationClient);
        return configClientService;
    }


    @Bean
    public StatisticsClientService statisticsClientService(NodeCommunicationClient nodeCommunicationClient){
        StatisticsClientServiceImpl statisticsClientService = new StatisticsClientServiceImpl();
        statisticsClientService.setNodeCommunicationClient(nodeCommunicationClient);
        return statisticsClientService;
    }
}
